#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2023/7/7 14:11
# @Author : 搬砖客
# @File : replace_load.py
# @Software: PyCharm

from typing import Text, Dict, Union
import json
from debug_talk import DebugTalk
from utils.process_files_utils.parameters_util import read_testcase_yaml


# 热加载
def replace_load(data: Union[Dict, Text]) -> Union[Dict, Text]:
    """

    :param data: 传入数据参数，可以是字典或文本类型
    :return: 对测试用例进行热加载
    """
    # 不管什么类型，统一转换为字符串格式
    if data and isinstance(data, dict):  # 如果data不为空，并且格式为字典,转换为字符串格式
        # ensure_ascii=False,避免将中文转换为Unicode编码，但是兼容可能会有问题
        # str_data = json.dumps(data, ensure_ascii=False)
        # 先将json字符串进行encode()编码，然后再使用unicode_escape编解码器将Unicode编码转换回中文字符
        str_data = json.dumps(data).encode().decode('unicode_escape')
    else:
        str_data = str(data)
    # 替换值
    if str_data is not None and "${" in str_data and "}" in str_data:
        for i in range(1, str_data.count("${") + 1):
            if "${" in str_data and "}" in str_data:
                start_index = str_data.index("${")
                end_index = str_data.index("}", start_index)
                # 字符串切片：切片从开始索引开始截取到结束索引，不包含结束索引（俗称含头不含尾），步长就是每几步取一个
                old_value = str_data[start_index:end_index + 1]
                func_name = old_value[2:old_value.index('(')]
                arg_value = old_value[old_value.index('(') + 1:old_value.index(')')]
                new_value = getattr(DebugTalk(), func_name)(*arg_value.split(','))
                str_data = str_data.replace(old_value, str(new_value))
        # 还原数据类型
        if data and isinstance(data, dict):  # 如果data为字典，则将str_data还原为字典
            data = json.loads(str_data)
        else:
            data = str_data
    # 返回替换过后的data
    return data


if __name__ == '__main__':
    args = read_testcase_yaml(r"\phm_platform\home_page\overview\1038.yaml")[0]
    args2 = read_testcase_yaml(r"/phm_platform/home_page/application_page/asset_mgmt_page/830.yaml")[0]
    args3 = read_testcase_yaml(r"/phm_platform/home_page/overview/1038.yaml")[0]
    expect_result = args['validate']
    if expect_result and isinstance(expect_result, list):
        for expect_value in expect_result:
            for key, value in dict(expect_value).items():
                value = replace_load(value)
                print(value)
